﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vnMcdonald.Models;

namespace vnMcdonald.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/
        vnMcdonaldContext db = new vnMcdonaldContext();
        ShoppingCart cart = new ShoppingCart();
        public ProductController(){
            
        }
        public ActionResult Index(int? CatId, int? Page)
        {
           
            var num_per = 4;
            Page = (Page == null) ? 0 : Page;
            //var Page = 3;
            var categoies = db.Categories.Where(c => c.IsActive == 1).OrderBy(c => c.Order).ToList();
            ViewBag.categoies = categoies;
            var product = db.Products.OrderByDescending(p => p.CreateDate).Skip((int)Page*num_per).Take(num_per);
            
            if (CatId != null)
            {
                ViewBag.catid = CatId;
                product = db.Products.Where(p => p.CategoryId == CatId).OrderByDescending(p => p.CreateDate).Skip((int)Page*num_per).Take(num_per);
            }
            if (Page > 0)
            {
                ViewBag.CurrentPage = Page;
            }
            return PartialView(product);
        }

        public ActionResult Detail(int ProductId)
        {
            var product = db.Products.Where(p => p.ProductId == ProductId && p.IsActive == 1).FirstOrDefault();
            if(Request.IsAjaxRequest()){
                return PartialView(product);
            }
            return View(product);
        }



    }
}
